* adjust file path
global dir "\data\"
cd "$dir"


*********************** Preparation of polling-station level election data *******************************
use election_data_2009.dta, clear

gen nrega_phase_3=0
replace nrega_phase_3=1 if nrega_phase_1==0 & nrega_phase_2==0

gen star_state=0
replace star_state=1 if state_code==8 | state_code==22 | state_code==23 | state_code==28 | state_code==33

gen int_eff=nrega_pred_1*state_rank_norm_1

* district FE
gen state_dist=state_code*100+dist_code

* parliamentary constituency fixed effects
gen pc_fe=(state_code*1000)+pc_code
duplicates drop

*********** Creation of outcome variables ****************
forval i=1(1)43 {
	cap replace totvotpoll`i'="" if totvotpoll`i'=="#REF!"
	cap replace totvotpoll`i'="" if totvotpoll`i'=="-"
	cap replace totvotpoll`i'="" if totvotpoll`i'=="    -"
	cap replace totvotpoll`i'="" if totvotpoll`i'=="_"
	cap replace totvotpoll`i'="0" if totvotpoll`i'==""
	cap gen byte notnumeric`i' = real(totvotpoll`i')==. 
	cap tab notnumeric`i' 
}

forval i=1(1)43 {
	cap drop if notnumeric`i'==1
	cap destring(totvotpoll`i'), replace
	cap replace totvotpoll`i'=totvotpoll`i'*(-1) if totvotpoll`i'<0
}


egen winner_temp = rowmax(totvotpoll*)
gen winner_id=.
gen winner_party=""
forval i=1(1)43 {
	replace winner_id=`i' if totvotpoll`i'==winner_temp
	replace winner_party=partyabbre`i' if totvotpoll`i'==winner_temp
}

gen congress_win=0
replace congress_win=1 if winner_party=="INC"

/* UPA 2004 members:
Indian National Congress (INC)
Rashtriya Janata Dal
Dravida Munnetra Kazhagam (DMK)
Nationalist Congress Party
Pattali Makkal Katchi
Telangana Rashtra Samithi
Jharkhand Mukti Morcha
Marumalarchi Dravida Munnetra Kazhagam (MDMK)
Lok Jan Shakti Party
Indian Union Muslim League
Jammu & Kashmir Peoples Democratic Party
Republican Party of India
All India Majlis-e-Ittehadul Muslimen
Kerala Congress
*/

gen upa_win=0
replace upa_win=1 if congress_win==1
replace upa_win=1 if winner_party=="RJD"
replace upa_win=1 if winner_party=="DMK"
replace upa_win=1 if winner_party=="NCP"
replace upa_win=1 if winner_party=="PMK"
replace upa_win=1 if winner_party=="TRS"
replace upa_win=1 if winner_party=="JMM"
replace upa_win=1 if winner_party=="MDMK"
replace upa_win=1 if winner_party=="LJP"
replace upa_win=1 if winner_party=="MUL"
replace upa_win=1 if winner_party=="JKPDP"
replace upa_win=1 if winner_party=="RPI"
replace upa_win=1 if winner_party=="AIMIM"
replace upa_win=1 if winner_party=="KEC"

gen bjp_win=0
replace bjp_win=1 if winner_party=="BJP"

forval i=1(1)43 {
gen upa_vote_share`i'=0
}

gen congress_vote_share=0
gen bjp_vote_share=0

replace ps_total_valid_votes="" if ps_total_valid_votes=="#REF!"
replace ps_total_votes="" if ps_total_votes=="#REF!"
replace ps_total_valid_votes="" if ps_total_valid_votes=="#VALUE!"
replace ps_total_votes="" if ps_total_votes=="#VALUE!"
replace ps_total_valid_votes="" if ps_total_valid_votes=="-"
replace ps_total_votes="" if ps_total_votes=="-"

replace ps_total_valid_votes = subinstr(ps_total_valid_votes,",","",.)
replace ps_total_votes = subinstr(ps_total_votes,",","",.)
replace ps_total_votes = subinstr(ps_total_votes,"*","",.)

replace ps_total_votes ="" if ps_total_votes=="Total"
replace ps_total_votes ="531" if ps_total_votes=="-531"
replace ps_total_votes ="307" if ps_total_votes=="0.307"

destring(ps_total_valid_votes), replace
destring(ps_total_votes), replace

replace ps_total_valid_votes=ps_total_votes if ps_total_valid_votes==.
egen tot_vot_temp=rowtotal(totvotpoll*)
replace ps_total_valid_votes=tot_vot_temp if ps_total_valid_votes==.

egen ps_total_new=rowtotal(totvotpoll*)

replace cand_vote_share1="" if cand_vote_share1=="delete"
forval i=1(1)43 {
destring(cand_vote_share`i'), replace
replace cand_vote_share`i'=(totvotpoll`i'/ps_total_new)*100
}

forval i=1(1)43 {
replace congress_vote_share=cand_vote_share`i' if partyabbre`i'=="INC"
replace upa_vote_share`i'=cand_vote_share`i' if partyabbre`i'=="INC" | partyabbre`i'=="RJD" | partyabbre`i'=="DMK" | partyabbre`i'=="NCP" | partyabbre`i'=="PMK" | partyabbre`i'=="MDMK" | partyabbre`i'=="LJP" | partyabbre`i'=="MUL" | partyabbre`i'=="RPI" | partyabbre`i'=="KEC" | partyabbre`i'=="TRS" | partyabbre`i'=="JMM" | partyabbre`i'=="JKPDP" | partyabbre`i'=="AIMIM" 
replace bjp_vote_share=cand_vote_share`i' if partyabbre`i'=="BJP"
}


egen upa_vote_share=rowtotal(upa_vote_share*)


**** state government at time of general elections ****
gen state_congress=0
replace state_congress=1 if state_code==28 | state_code==18 | state_code==6 | state_code==27 | state_code==8

gen state_upa=0
replace state_upa=1 if state_congress==1
replace state_upa=1 if state_code==33
rename state_upa upa_state

save election_sample.dta, replace




*********************** Preparation of constituency-level election data *******************************

clear
clear matrix

use general_election_const.dta, clear

* only keep if have the underlying running variable
drop if rank==.

* district FE
gen state_dist=state_code*100+dist_code

gen congress_win=0
replace congress_win=1 if  partyabbre1=="INC"

gen upa_win=0
replace upa_win=1 if congress_win==1
replace upa_win=1 if partyabbre1=="RJD"
replace upa_win=1 if partyabbre1=="DMK"
replace upa_win=1 if partyabbre1=="NCP"
replace upa_win=1 if partyabbre1=="PMK"
replace upa_win=1 if partyabbre1=="TRS" 
replace upa_win=1 if partyabbre1=="JMM" 
replace upa_win=1 if partyabbre1=="MDMK"
replace upa_win=1 if partyabbre1=="LJP"
replace upa_win=1 if partyabbre1=="MUL"
replace upa_win=1 if partyabbre1=="JKPDP" 
replace upa_win=1 if partyabbre1=="RPI" 
replace upa_win=1 if partyabbre1=="AIMIM" 
replace upa_win=1 if partyabbre1=="KEC"

gen bjp_win=0
replace bjp_win=1 if partyabbre1=="BJP"

forval i=1(1)43 {
gen upa_vote_share`i'=0
}

gen congress_vote_share=0
gen bjp_vote_share=0

forval i=1(1)43 {
replace congress_vote_share=cand_vote_share`i' if partyabbre`i'=="INC"
replace upa_vote_share`i'=cand_vote_share`i' if partyabbre`i'=="INC" | partyabbre`i'=="RJD" | partyabbre`i'=="DMK" | partyabbre`i'=="NCP" | partyabbre`i'=="PMK" | partyabbre`i'=="MDMK" | partyabbre`i'=="LJP" | partyabbre`i'=="MUL" | partyabbre`i'=="RPI" | partyabbre`i'=="KEC" | partyabbre`i'=="TRS" | partyabbre`i'=="JMM" | partyabbre`i'=="JKPDP" | partyabbre`i'=="AIMIM" 
replace bjp_vote_share=cand_vote_share`i' if partyabbre`i'=="BJP"
}

egen upa_vote_share=rowtotal(upa_vote_share*)

** voter turnout
gen voter_turnout=total_voters/total_electors

gen congress_win_2004=0 if partyabbre1_2004!=""
replace congress_win_2004=1 if  partyabbre1_2004=="INC"

gen upa_win_2004=0 if partyabbre1_2004!=""
replace upa_win_2004=1 if congress_win_2004==1
replace upa_win_2004=1 if partyabbre1_2004=="RJD"
replace upa_win_2004=1 if partyabbre1_2004=="DMK"
replace upa_win_2004=1 if partyabbre1_2004=="NCP"
replace upa_win_2004=1 if partyabbre1_2004=="PMK"
replace upa_win_2004=1 if partyabbre1_2004=="TRS" 
replace upa_win_2004=1 if partyabbre1_2004=="JMM" 
replace upa_win_2004=1 if partyabbre1_2004=="MDMK"
replace upa_win_2004=1 if partyabbre1_2004=="LJP"
replace upa_win_2004=1 if partyabbre1_2004=="MUL"
replace upa_win_2004=1 if partyabbre1_2004=="JKPDP" 
replace upa_win_2004=1 if partyabbre1_2004=="RPI" 
replace upa_win_2004=1 if partyabbre1_2004=="AIMIM" 
replace upa_win_2004=1 if partyabbre1_2004=="KEC"

gen bjp_win_2004=0 if partyabbre1_2004!=""
replace bjp_win_2004=1 if partyabbre1_2004=="BJP"

forval i=1(1)35 {
gen upa_vote_share_2004`i'=0 if partyabbre1_2004!=""
}

gen congress_vote_share_2004=0 if partyabbre1_2004!=""
gen bjp_vote_share_2004=0 if partyabbre1_2004!=""
forval i=1(1)35 {
replace congress_vote_share_2004=cand_vote_share`i'_2004 if partyabbre`i'_2004=="INC"
replace upa_vote_share_2004`i'=cand_vote_share`i'_2004 if partyabbre`i'_2004=="INC" | partyabbre`i'_2004=="RJD" | partyabbre`i'_2004=="DMK" | partyabbre`i'_2004=="NCP" | partyabbre`i'_2004=="PMK" | partyabbre`i'_2004=="MDMK" | partyabbre`i'_2004=="LJP" | partyabbre`i'_2004=="MUL" | partyabbre`i'_2004=="RPI" | partyabbre`i'_2004=="KEC" | partyabbre`i'_2004=="TRS" | partyabbre`i'_2004=="JMM" | partyabbre`i'_2004=="JKPDP" | partyabbre`i'_2004=="AIMIM" 
replace bjp_vote_share_2004=cand_vote_share`i'_2004 if partyabbre`i'_2004=="BJP"
}

egen upa_vote_share_2004=rowtotal(upa_vote_share_2004*) if partyabbre1_2004!=""

replace congress_vote_share_2004=congress_vote_share_2004*100
replace upa_vote_share_2004=upa_vote_share_2004*100
replace bjp_vote_share_2004=bjp_vote_share_2004*100

** voter turnout
gen voter_turnout_2004=tot_voters_2004/tot_electors_2004 if partyabbre1_2004!=""

gen leader2004_sc=0 if cand_category1_2004!=""
replace leader2004_sc=1 if cand_category1_2004=="SC"

gen leader2004_age=cand_age1_2004

gen leader2004_namelength=strlen(cand_name1_2004)

gen dummy=1 if nrega_pred_1==0
replace dummy=0 if nrega_pred_1==1

save election_sample_const.dta, replace
